//
//  DDIndustrySelectViewController.m
//  DingDongZP
//
//  Created by Ray on 16/4/22.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDIndustrySelectViewController.h"
#import "DDCommonSelectionCell.h"
#import "DDIndustry.h"

@interface DDIndustrySelectViewController ()

@property (nonatomic, strong) NSArray *industries;

@end

@implementation DDIndustrySelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"公司行业";
    [self.tableView registerNib:[UINib nibWithNibName:@"DDCommonSelectionCell"
                                               bundle:nil]
         forCellReuseIdentifier:@"cell"];
    [self.tableView reloadData];
    
    self.navigationItem.rightBarButtonItem = self.confirmItem;
    
    [self loadIndustry];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - events
- (void)actionConfirm:(UIButton *)btn
{
    if ([self.tableView indexPathForSelectedRow]) {
        NSIndexPath *spath = [self.tableView indexPathForSelectedRow];
        if (spath.row < self.industries.count) {
            DDIndustry *industry = self.industries[spath.row];
            if (self.didSelectIndustry) {
                self.didSelectIndustry(industry);
            }
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - functions
- (void)loadIndustry
{
    NSString *apiname = @"query_industry";
    NSMutableDictionary *params = [NSMutableDictionary new];
    NSString *URLString = [[DDAPIGenerate sharedInstance] API:apiname];
    
    DDNetWorkManager *manager = [DDNetWorkManager shareManager];
    WEAKSELF
    [manager POST:URLString parameters:params success:^(id responseObject) {
        if ([weakSelf isValidateResponseData:responseObject]) {
            NSArray *datas = responseObject[@"data"];
            NSArray *models = [NSArray yy_modelArrayWithClass:[DDIndustry class] json:datas];
            weakSelf.industries = models;
            [weakSelf.tableView reloadData];
            
            /// 偷懒法设置选中项
            if (weakSelf.selectIndustryName) {
                for (int i = 0; i < models.count; i++) {
                    DDIndustry *d = models[i];
                    if ([d.industry_name isEqualToString:self.selectIndustryName]) {
                        [weakSelf.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
                        break;
                    }
                }
            }
        }
        else {
            [weakSelf responseUnValidate:responseObject];
        }
    } failure:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.industries.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDCommonSelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row < self.industries.count) {
        DDIndustry *d = self.industries[indexPath.row];
        cell.nameLab.text = d.industry_name;
        
//        if ([d.industry_name isEqualToString:self.selectIndustryName]) {
//            cell.selected = YES;
//        }
//        else {
//            cell.selected = NO;
//        }
    }
    else {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.text = @"我找不到符合的行业";
        return cell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.industries.count) {
        //  do nothing...
        if ([self.tableView indexPathForSelectedRow]) {
            NSIndexPath *spath = [self.tableView indexPathForSelectedRow];
            if (spath.row < self.industries.count) {
                DDIndustry *industry = self.industries[spath.row];
                if (self.didSelectIndustry) {
                    self.didSelectIndustry(industry);
                }
            }
        }
        [self.navigationController popViewControllerAnimated:YES];

        
    }
    else {
        //  enter a new industry!
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"输入行业名称，不超过10个字" preferredStyle:UIAlertControllerStyleAlert];
        [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            
        }];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
